package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"repo-sys/cache"
	"repo-sys/model"
	"strconv"
	"time"
)

func WareHouseAdd(c *gin.Context) {
	WareCode := c.PostForm("WareCode")
	WareName := c.PostForm("WareName")
	data := model.Warehouse{
		WareCode: WareCode,
		WareName: WareName,
	}
	err := model.DB.Debug().Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func OrderHouseAdd(c *gin.Context) {
	OrderCode := c.PostForm("OrderCode")
	OrderName := c.PostForm("OrderName")
	GoodNum, _ := strconv.Atoi(c.PostForm("GoodNum"))
	Wid, _ := strconv.Atoi(c.PostForm("Wid"))
	data := model.OrderHouse{
		OrderCode: OrderCode,
		OrderName: OrderName,
		GoodNum:   GoodNum,
		Wid:       Wid,
	}
	err := model.DB.Debug().Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func OrderHouseList(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	/*
		SELECT
		  order_houses.id AS OrderId,
		  order_houses.order_code AS OrderCode,
		  order_houses.order_name AS OrderName,
		  order_houses.good_num AS GoodNum,
		  warehouses.ware_code AS WareCode,
		  warehouses.ware_name AS WareName
		FROM
		  order_houses
		  JOIN warehouses ON order_houses.wid = warehouses.id
	*/
	redis_key := "OrderHouse_List" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	var orderHouseList []model.OrderHouseList
	if cacheData == "" {
		model.DB.Table("order_houses").
			Select("order_houses.id AS OrderId",
				"order_houses.order_code AS OrderCode",
				"order_houses.order_name AS OrderName",
				"order_houses.good_num AS GoodNum",
				"warehouses.ware_code AS WareCode",
				"warehouses.ware_name AS WareName",
			).
			Joins("JOIN warehouses ON order_houses.wid = warehouses.id").
			Offset(offset).Limit(size).
			Debug().Find(&orderHouseList)
		jsonData, _ := json.Marshal(&orderHouseList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Second*5)
	} else {
		json.Unmarshal([]byte(cacheData), &orderHouseList)
	}
	var count int64
	model.DB.Model(&model.OrderHouse{}).Count(&count)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "展示成功",
		"data":  orderHouseList,
		"count": count,
	})
	return
}
func OrderHouseDetail(c *gin.Context) {
	id := c.Query("id")
	redis_key := "Order_Detail" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	var orderHouseList []model.OrderHouseList
	if cacheData == "" {
		model.DB.Table("order_houses").
			Select("order_houses.id AS OrderId",
				"order_houses.order_code AS OrderCode",
				"order_houses.order_name AS OrderName",
				"order_houses.good_num AS GoodNum",
				"warehouses.ware_code AS WareCode",
				"warehouses.ware_name AS WareName",
			).
			Joins("JOIN warehouses ON order_houses.wid = warehouses.id").
			Where("order_houses.id=?", id).
			Debug().Find(&orderHouseList)
		jsonData, _ := json.Marshal(&orderHouseList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &orderHouseList)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "展示成功",
		"data": orderHouseList,
	})
	return
}
